package com.yiwei.zxp.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.MyShareMoneyAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.ShareGoldListBean;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.view.OnLoadMoreBottomListener;
import com.yiwei.zxp.view.OnLoadeMoreListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-5-8.
 */

public class MyShareMoneyFragment extends BaseFragment {
    @BindView(R.id.list)
    RecyclerView list;

    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private MyShareMoneyAdapter adapter;
    private int id;
    private OnLoadMoreBottomListener listener;
    private boolean isNoData = false;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my_share_money;
    }

    @Override
    protected void initView() {
        if (getArguments() != null) {
            id = getArguments().getInt("id", 0);
        }
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                listener.setPage(1);
                getShareGoldList(1);
            }
        });
        adapter = new MyShareMoneyAdapter(getContext());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        listener = new OnLoadMoreBottomListener() {
            @Override
            public void onLoadMore(int page) {
                getShareGoldList(page);
            }

            @Override
            public void onBottom() {
                if(isNoData)
                ToastUtil.makeText(getContext(), "到底啦！");
            }
        };
        list.addOnScrollListener(listener);
        list.setLayoutManager(layoutManager);
        list.setHasFixedSize(true);
        list.setAdapter(adapter);


        getShareGoldList(1);
    }

    public void getShareGoldList(int page) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);
        ShareGoldListBean bean = new ShareGoldListBean();
        bean.current_page = page;
        bean.per_page = 10;
        bean.status = 0;
        Api.api_service.getShareGoldList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                    if (result.datas == null || result.datas.size() == 0) {
                        isNoData = true;
                    } else {
                        isNoData = false;
                    }
                });
    }


}
